package com.zhang.queue;

import java.util.Arrays;

/**
 * @author zhang
 * @time 2022/03/07 21:35:42
 */
public class ArrayQueue {
    private int[] array = new int[5];
    private int rear;
    private int front;
    private int count;


    public void enqueue(int item) {
        if (isFull()) {
            throw new IllegalArgumentException();
        }
        array[rear] =item;
        rear = (rear + 1) % array.length;
        count++;
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException();
        }
        int item = array[front];
        array[front] = 0;
        front = (front + 1) % array.length;
        count--;
        return item;
    }

    public int peek() {
        if (isEmpty()) {
            throw new IllegalArgumentException();
        }
        return array[front];
    }
    public boolean isEmpty() {
        return count == 0;
    }

    public boolean isFull() {
        return count == array.length;
    }

    @Override
    public String toString() {
        return Arrays.toString(array);
    }
}
